// Controller.cpp: implementation of the Mouse class.
//
//////////////////////////////////////////////////////////////////////

#include "Controller.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Controller::Controller()
{

	// No devices found, by now...
	this->devices = 0x0;

	/* PLIB Joystick initialization */
	jsInit () ;

	#ifdef LOG
	printf("-------------------------------- CONTROLLER ------------------------------------\n");
	printf("\nProbing input devices...\n\n");
	#endif

	// Create joystick & test if present and working well
	this->js = new Joystick(PLIB_JOYSTICK_NUMBER); if(js->isWorking()) this->devices |= CONTROLLER_JOYSTICK;
	// Create keyboard & test if present and working well
	this->kb = new Keyboard(); if(kb->isWorking()) this->devices |= CONTROLLER_KEYBOARD;
	// Create mouse & test if present and working well
	this->ms = new Mouse(); if(ms->isWorking()) this->devices |= CONTROLLER_MOUSE;

	// printf the devices detected...
	#ifdef LOG
	printDeviceStatus();
	#endif
}

Controller::~Controller()
{
delete this->js;
delete this->kb;
delete this->ms;
}

// Event driven functions
void Controller::readKeyPress( int key, int updn, int x, int y )
{
  // update the key status array
  if((this->devices & CONTROLLER_KEYBOARD)) kb->read(key,updn,x,y);
}

void Controller::readMousePosition( int x, int y )
{
  // Update the position array
  if((this->devices & CONTROLLER_MOUSE)) ms->locatePointer(x,y);
}

void Controller::readMouseButtons( int button, int updn, int x, int y )
{
  // Update the button status array
  if((this->devices & CONTROLLER_MOUSE)) ms->readButtons(button,updn,x,y);
}

// Polling functions
void Controller::readMouse()
{
  // scan the mouse position....
 
  int mr = ms->isDirection(MOUSE_RIGHT); // positive value
  int ml = ms->isDirection(MOUSE_LEFT);  // negative value
  int mu = ms->isDirection(MOUSE_UP);    // positive value
  int md = ms->isDirection(MOUSE_DOWN);  // negative value
 
  #ifdef CONSOLE
  #ifdef DEBUG
  if ( (mr!=0) || (ml!=0) || (mu!=0) || (md!=0) ) printf("MOUSE: ");
  #endif
  #endif
 
  if( mr != 0)
	{
    #ifdef CONSOLE
    #ifdef DEBUG
	printf("RIGHT(%d)",mr);
	#endif
	#endif
	}
  if( ml != 0)
	{
    #ifdef CONSOLE
    #ifdef DEBUG
	printf("LEFT(%d)",ml);
	#endif
	#endif
	}
  if( mu != 0)
	{
    #ifdef CONSOLE
    #ifdef DEBUG
	printf("UP(%d)",mu);
	#endif
	#endif
	}
  if( md != 0)
	{
    #ifdef CONSOLE
    #ifdef DEBUG
	printf("DOWN(%d)",md);
	#endif
	#endif
	}
/* Read buttons */
#ifdef CONSOLE
#ifdef DEBUG
int mbp = 0;
#endif
#endif

if( ms->isPressed(MOUSE_BUTTON_LEFT) )
    {
    #ifdef CONSOLE
    #ifdef DEBUG
    mbp = 1;
    printf( "LEFT_BUTTON" );
	#endif
	#endif
    }
if( ms->isPressed(MOUSE_BUTTON_RIGHT) )
    {
    #ifdef CONSOLE
    #ifdef DEBUG
    mbp = 1;
    printf( "RIGHT_BUTTON" );
	#endif
	#endif
    }
if( ms->isPressed(MOUSE_BUTTON_CENTER) )
    {
    #ifdef CONSOLE
    #ifdef DEBUG
    mbp = 1;
    printf( "CENTER_BUTTON" );
	#endif
	#endif
    }
#ifdef CONSOLE
#ifdef DEBUG
if ( (mr!=0) || (ml!=0) || (mu!=0) || (md!=0) || mbp) printf("\n");
#endif
#endif
ms->clear();
}

int Controller::readKeyboard()
{
return kb->read();
}

void Controller::readJoystick()
{
  // Read data from joystick
  js->read();

  // Calls the 'logical' function connected to the command
  // So I can call the right function 
  if(js->isWorking())
  {
  if(js->isPressed(JOYSTICK_A)) 
	{
	  
	}
  if(js->isPressed(JOYSTICK_B)) 
	{
	 
	}
  if(js->isPressed(JOYSTICK_X)) 
	{
	 
	}
  if(js->isPressed(JOYSTICK_Y)) 
	{
	 
	}
  if(js->isPressed(JOYSTICK_L)) 
	{
	 
	}
  if(js->isPressed(JOYSTICK_R)) 
	{ 
	 
	}

  // The same as above but for the control pad
  if(js->isDirection(JOYSTICK_UP))
	{

	}
  if(js->isDirection(JOYSTICK_DOWN))
	{

	}
  if(js->isDirection(JOYSTICK_LEFT))
	{

	}
  if(js->isDirection(JOYSTICK_RIGHT))
	{

	}
  if(js->isDirection(JOYSTICK_H_CENTERED))
	{

	}
  if(js->isDirection(JOYSTICK_V_CENTERED))
	{

	}

  }
}

void Controller::printDeviceStatus()
{
  #ifdef CONSOLE
  printf("\n");
  printf("                           Controller Device Status\n");
  printf("                           ------------------------\n");
  printf("                            KB        MS        JS \n");
  printf("                           ");
  if(this->devices & CONTROLLER_KEYBOARD)	printf(" OK "); else printf(" NO ");
  											printf("      "); 
  if(this->devices & CONTROLLER_MOUSE)  	printf(" OK "); else printf(" NO ");
											printf("      "); 
  if(this->devices & CONTROLLER_JOYSTICK)	printf(" OK "); else printf(" NO ");
											printf("      "); 
  printf("\n");
  #endif
}

int Controller::readInputs()
{
  int r = 1;
  // Read inputs only if the device is enabled!!!
  if(this->devices & CONTROLLER_JOYSTICK)	this->readJoystick();
  if(this->devices & CONTROLLER_MOUSE)	    this->readMouse();
  if(this->devices & CONTROLLER_KEYBOARD)	r = this->readKeyboard();
  // r = 0 means EXIT!
  return r;
}

